home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1997 May / PC Plus Super CD Issue 127 (May 1997).iso / handson / delphi / encrypt / textio.pas < prev   
Encoding:
Pascal/Delphi Source File  |  1997-02-03  |  2.5 KB  |  96 lines

  1. unit Textio;
  2. { PC Plus sample program to illustrate text file IO.                  }
  3. { It opens one file for reading, another for writing and copies the   }
  4. { contents, one char at a time, between the files.                    }
  5. { Just to add to the fun, it also 'encrypts' the text en route.       }
  6. { The encryption is *very* simple. So don't bother trying to sell     }
  7. { this program to the CIA!                                            }
  8.  
  9. interface
  10.  
  11. uses
  12.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  13.   Forms, Dialogs, StdCtrls;
  14.  
  15. type
  16.   TForm1 = class(TForm)
  17.     EncryptBtn: TButton;
  18.     DecryptBtn: TButton;
  19.     procedure EncryptBtnClick(Sender: TObject);
  20.     procedure DecryptBtnClick(Sender: TObject);
  21.   private
  22.     { Private declarations }
  23.   public
  24.     { Public declarations }
  25.   end;
  26.  
  27. const
  28.    MAGICNUM = 17; { this is an arbitrary 'encryption factor' }
  29.  
  30. var
  31.   Form1: TForm1;
  32.  
  33. implementation
  34.  
  35. {$R *.DFM}
  36.  
  37. procedure TForm1.EncryptBtnClick(Sender: TObject);
  38. var
  39.    infile,
  40.    outfile     : TextFile;
  41.    INfilename,
  42.    OUTfilename : string;
  43.    ch          : char;
  44. begin
  45.   INfilename  := 'TextIO.pas';
  46.   OUTfilename := 'Encrypt.txt';
  47.   if not FileExists( INfilename ) then     { Check that input file exists }
  48.       ShowMessage('File: ' + INfilename + ' not found!')
  49.   else
  50.   begin
  51.     AssignFile(infile, INfilename);
  52.     Reset(infile);
  53.     AssignFile(outfile, OUTfilename);
  54.     Rewrite(outfile);
  55.     while not Eof(infile) do
  56.     begin
  57.       Read(infile, ch);
  58.       { Write(outfile, ch); }
  59.       Write(outfile, Chr(Ord(ch)+MAGICNUM));
  60.     end;
  61.     CloseFile(outfile);
  62.     CloseFile(infile);
  63.   end;
  64. end;
  65.  
  66. procedure TForm1.DecryptBtnClick(Sender: TObject);
  67. var
  68.    infile,
  69.    outfile     : TextFile;
  70.    INfilename,
  71.    OUTfilename : string;
  72.    ch          : char;
  73. begin
  74.   INfilename  := 'Encrypt.txt';
  75.   OUTfilename := 'Decrypt.txt';
  76.   if not FileExists( INfilename ) then     { Check that input file exists }
  77.       ShowMessage('File: ' + INfilename + ' not found!')
  78.   else
  79.   begin
  80.     AssignFile(infile, INfilename);
  81.     Reset(infile);
  82.     AssignFile(outfile, OUTfilename);
  83.     Rewrite(outfile);
  84.     while not Eof(infile) do
  85.     begin
  86.       Read(infile, ch);                      (* To do a straight file copy   *)
  87.       { Write(outfile, ch); }                (* Uncomment this line of code  *)
  88.       Write(outfile, Chr(Ord(ch)-MAGICNUM)); (* and comment out this line    *)
  89.     end;
  90.     CloseFile(outfile);
  91.     CloseFile(infile);
  92.   end;
  93. end;
  94.  
  95. end.
  96.